#include "registerdialog.h"

#include "global.h"
#include "httpmgr.h"

RegisterDialog::RegisterDialog(QWidget *parent) :
    QDialog(parent)
{
    setObjectName("registerDialog");

    setupUi();
    connect(HttpMgr::GetInstance().get(), &HttpMgr::sig_reg_mod_finish, this, &RegisterDialog::slot_reg_mod_finished);

    initHttpHandlers();
}

void RegisterDialog::setupUi()
{
    QVBoxLayout *mainVLayout = new QVBoxLayout;
    mainVLayout->setContentsMargins(10, 10, 10, 10);
    mainVLayout->addStretch();

    m_errTipLabel = new QLabel("错误提示");
    m_errTipLabel->setObjectName("errTipLabel");
    //水平居中显示
    m_errTipLabel->setAlignment(Qt::AlignCenter);
    m_errTipLabel->setProperty("state", "normal");
    mainVLayout->addWidget(m_errTipLabel);
    repolish(m_errTipLabel);

    m_stackedWidget = new QStackedWidget();
    m_stackedWidget->addWidget(createRegisterPage());
    mainVLayout->addWidget(m_stackedWidget);

    this->setLayout(mainVLayout);
}

QWidget * RegisterDialog::createRegisterPage(void)
{
    QWidget * registerWidget = new QWidget();

    QVBoxLayout * mainVLayout = new QVBoxLayout;
    mainVLayout->setContentsMargins(0, 0, 0, 0);
    registerWidget->setLayout(mainVLayout);

    QHBoxLayout * userHLayout = new QHBoxLayout;
    userHLayout->setContentsMargins(0, 0, 0, 0);
    QLabel * userLabel = new QLabel("用户：");
    userLabel->setObjectName("userRegLabel");
    userRegLineEdit = new QLineEdit();
    userRegLineEdit->setObjectName("userRegLineEdit");
    userHLayout->addWidget(userLabel);
    userHLayout->addWidget(userRegLineEdit);
    mainVLayout->addLayout(userHLayout);

    QHBoxLayout * pwdHLayout = new QHBoxLayout;
    pwdHLayout->setContentsMargins(0, 0, 0, 0);
    QLabel * pwdLabel = new QLabel("密码：");
    pwdLabel->setObjectName("pwdRegLabel");
    pwdRegLineEdit = new QLineEdit();
    pwdRegLineEdit->setObjectName("pwdRegLineEdit");
    pwdRegLineEdit->setEchoMode(QLineEdit::Password);
    pwdHLayout->addWidget(pwdLabel);
    pwdHLayout->addWidget(pwdRegLineEdit);
    mainVLayout->addLayout(pwdHLayout);

    QHBoxLayout * confirmHLayout = new QHBoxLayout;
    confirmHLayout->setContentsMargins(0, 0, 0, 0);
    QLabel * confirmLabel = new QLabel("确认：");
    confirmLabel->setObjectName("confirmRegLabel");
    pwdConfirmRegLineEdit = new QLineEdit();
    pwdConfirmRegLineEdit->setObjectName("confirmRegLineEdit");
    pwdConfirmRegLineEdit->setEchoMode(QLineEdit::Password);
    confirmHLayout->addWidget(confirmLabel);
    confirmHLayout->addWidget(pwdConfirmRegLineEdit);
    mainVLayout->addLayout(confirmHLayout);

    QHBoxLayout * emailHLayout = new QHBoxLayout;
    emailHLayout->setContentsMargins(0, 0, 0, 0);
    QLabel * emailLabel = new QLabel("邮箱：");
    emailLabel->setObjectName("emailRegLabel");
    emailRegLineEdit = new QLineEdit();
    emailRegLineEdit->setObjectName("emailRegLineEdit");
    emailHLayout->addWidget(emailLabel);
    emailHLayout->addWidget(emailRegLineEdit);
    mainVLayout->addLayout(emailHLayout);

    QHBoxLayout * varifyHLayout = new QHBoxLayout;
    varifyHLayout->setContentsMargins(0, 0, 0, 0);
    QLabel * varifyLabel = new QLabel("验证码：");
    varifyLabel->setObjectName("varifyRegLabel");
    varifyRegLineEdit = new QLineEdit();
    varifyRegLineEdit->setObjectName("varifyRegLineEdit");
    varifyRegLineEdit->setPlaceholderText("请输入验证码");
    QPushButton * varifyRegButton = new QPushButton("获取");
    varifyRegButton->setObjectName("varifyRegButton");
    varifyHLayout->addWidget(varifyLabel);
    varifyHLayout->addWidget(varifyRegLineEdit);
    varifyHLayout->addWidget(varifyRegButton);
    mainVLayout->addLayout(varifyHLayout);
    mainVLayout->addStretch();

    QHBoxLayout * buttonHLayout = new QHBoxLayout;
    buttonHLayout->setContentsMargins(0, 0, 0, 0);
    QPushButton * confirmRegButton = new QPushButton("确认");
    confirmRegButton->setObjectName("confirmRegButton");
    QPushButton * cancelRegButton = new QPushButton("取消");
    cancelRegButton->setObjectName("cancelRegButton");
    buttonHLayout->addWidget(confirmRegButton);
    buttonHLayout->addWidget(cancelRegButton);
    mainVLayout->addLayout(buttonHLayout);
    mainVLayout->addStretch();

    connect(varifyRegButton, &QPushButton::clicked, this, [this](){
        auto email = emailRegLineEdit->text();
        QRegularExpression regex(R"((\w+)(\.|_)?(\w*)@(\w+)(\.(\w+))+)");
        bool match = regex.match(email).hasMatch();
        if (match){
            //发送验证码
        }else{
            showTip(tr("邮箱格式错误"), false);
        }
    });

    return registerWidget;
}

void RegisterDialog::showTip(const QString &tip, bool isOk)
{
    m_errTipLabel->setProperty("state", isOk ? "normal" : "error");
    repolish(m_errTipLabel);
    m_errTipLabel->setText(tip);
}


void RegisterDialog::slot_reg_mod_finished(ReqId req_id, const QString & res, ErrorCodes err)
{
    if (err != ErrorCodes::SUCCESS){
        showTip(tr("网络请求错误"), false);
        return;
    }

    //解析json
    QJsonDocument jsonDoc = QJsonDocument::fromJson(res.toUtf8());
    if (jsonDoc.isNull()){
        showTip(tr("JSON解析错误"), false);
        return;
    }
    if (!jsonDoc.isObject()){
        showTip(tr("JSON格式错误"), false);
        return;
    }
    m_httpHandlers[req_id](jsonDoc.object());
}

void RegisterDialog::initHttpHandlers()
{
    //注册获取验证码
    m_httpHandlers.insert(ReqId::ID_GET_VARIFY_CODE, [this](const QJsonObject &jsonObj){
        int error = jsonObj["error"].toInt();
        if (error != ErrorCodes::SUCCESS){
            showTip(tr("参数请求失败!"), false);
            return;
        }

        auto email = jsonObj["email"].toString();
        showTip(tr("验证码已发送到邮箱，注意查收!"), true);
        qDebug() << "email:" << email;

    });
}